package high.concurrent.operation;

//import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;

@RestController
public class CreateTopic {

    @Autowired(required = false)
    private AdminClient adminClient;

    /**
     * 创建 topic
     * 写法和test下的com.study.kafka.topic.KafkaCreateTopic.java完全一致，只不过把连接注入到了spring容器，通过@Autowired方式获取
     * @param topicName
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("spring-kafka/createTopic")
    public void createTopic(@RequestParam(value = "topicName") String topicName) throws ExecutionException, InterruptedException {

        CreateTopicsResult createTopicsResult = adminClient.createTopics(Arrays.asList(new NewTopic(topicName, 3, (short) 1)));

        createTopicsResult.all().get();

    }
}
